package dao;

import net.sf.json.JSONObject;
import portal.mybatis.dao.service.CompanyService;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CompanyDao {
	@Autowired
	CompanyService companyService;
	public boolean addCompany(String name, String roleId){
		return companyService.insert(name, roleId);
	}
//	@Deprecated
//    public boolean addCompany1(String name,String roleId){
//        String sql = "INSERT INTO `company` (name,role_id) VALUES (?,(SELECT * FROM " +
//                "(SELECT id from `role` where id = ?) AS a))";
//        return Query.executeDML(sql, new Object[]{name,roleId})>0;
//    }
	public boolean addCompany(String name){
		return addCompany(name,null);
    }
//	@Deprecated
//	public boolean addCompany1(String name){
//        String sql = "INSERT INTO `company` (name) VALUES (?)";
//        return Query.executeDML(sql, new Object[]{name})>0;
//    }
	public boolean deleteCompany(String id) {
		try{
			return companyService.deleteById(Long.valueOf(id));
		}catch(Exception e){		
			e.printStackTrace();
		}
		return false;
	}	
//	@Deprecated
//    public boolean deleteCompany1(String id) {
//        String sql = "delete from company where id = ?";
//        return Query.executeDML(sql, new Object[]{id})>0;
//    }
	public boolean alterCompany(String companyId, String name, String roleId) {
		return companyService.update(name, roleId, companyId);
	}
	public boolean alterCompany(String companyId, String name) {
		return companyService.update(name, null, companyId);
	}
//@Deprecated
//    public boolean alterCompany1(String companyId, String name, String roleId) {
////        String sql = "update company set name = ? ,role_id = (SELECT * FROM (SELECT id from `role` where id = ?) AS a)";
//        String sql = "update company set name = ? ,role_id = ? where id = ?";
//        return Query.executeDML(sql, new Object[]{name,roleId,companyId})>0;
//    }
//@Deprecated
//    public boolean alterCompany1(String companyId, String name) {
////        String sql = "update company set name = ? ,role_id = (SELECT * FROM (SELECT id from `role` where id = ?) AS a)";
//        String sql = "update company set name = ? where id = ?";
//        return Query.executeDML(sql, new Object[]{name,companyId})>0;
//    }
   /* public List<Object> showAllCompany(Integer onePageCount, Integer currentPage) {
        String sql = "";
        if(onePageCount==0&&currentPage==0){
            sql = "SELECT company.id,company.name,role.id as roleId,role.name as roleName " +
                    "from company,role where company.role_id=role.id order by id desc";
        }else{
            sql = "SELECT company.id,company.name,role.id as roleId,role.name as roleName " +
                    "from company,role where company.role_id=role.id order by id desc limit "
                    + (currentPage - 1) * onePageCount + " , " + onePageCount;
        }
        final List<Object> result = new ArrayList<>();
        return this.companyDetail(sql,null);
    }*/
	public List<Object> showAllCompany(Integer onePageCount, Integer currentPage) {
		if(currentPage<1)
			currentPage=1;
		return companyService.selectByPage(Long.valueOf((currentPage*onePageCount)-onePageCount), Long.valueOf(onePageCount));
	}
//	@Deprecated
//    public List<Object> showAllCompany1(Integer onePageCount, Integer currentPage) {
//        String sql = "";
//        if(onePageCount==0&&currentPage==0){
//            sql = "SELECT company.id,company.name from company order by id desc";
//        }else{
//            sql = "SELECT company.id,company.name from company order by id desc limit "+
//                    + (currentPage - 1) * onePageCount + " , " + onePageCount;
//        }
//        final List<Object> result = new ArrayList<>();
//        return this.companyDetail(sql,null);
//    }
	public long companyNum(){
		return companyService.countByPage(0L, 0L);
	}
//    public synchronized int companyNum1() {
//        String sql = "select count(*) from company";
//        return PublicDao.oneNum(sql,null);
//    }

    public long companyPageNum(Integer onePageCount){
        long companyNum = this.companyNum();
        return (companyNum % onePageCount)==0? (companyNum / onePageCount) : (companyNum / onePageCount + 1);
    }
    public List<Object> showOneCompany(String companyId){
    	return companyService.selectByX(companyId, null, null);
    }
//    public List<Object> showOneCompany1(String companyId){
//        String sql = "SELECT company.id,company.name from company where company.id = ?";
//        Object[] params = new Object[]{companyId};
//        return this.companyDetail(sql, params);
//    }
    public JSONObject companyDetailByName(String name){
    	return companyService.selectOne(null, name, null);
    }
//    @Deprecated
//    public JSONObject companyDetailByName1(String name){
//        String sql = "select * from company where  name =?";
//        return (JSONObject) companyDetail(sql,new Object[]{name}).get(0);
//    }
//    public List<Object> companyDetail(String sql, Object[] params) {
//        List<Object> result = new ArrayList<>();
//        return Query.executeQueryList(result, sql, params, new QueryCallBackList() {
//            @Override
//            public List<Object> doExecuteList(ResultSet rs, List<Object> result) {
//                try {
//                    while(rs.next()){
//                        JSONObject oneCompany = new JSONObject();
//                        oneCompany.put("companyId",rs.getString("id"));
//                        oneCompany.put("companyName",rs.getString("name"));
////                        oneCompany.put("roleId",rs.getString("roleId"));
////                        oneCompany.put("roleName",rs.getString("roleName"));
//                        result.add(oneCompany);
//                    }
//                    return result;
//                } catch (SQLException e) {
//                    e.printStackTrace();
//                    return null;
//                }
//            }
//        });
//    }
}
